package com.atguigu.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import sun.dc.pr.PRError;

import javax.sql.DataSource;
import java.beans.PersistenceDelegate;

/**
 * @Author：请允悲
 * @Slogan：致敬大师
 * @Package：com.atguigu.config
 * @Project：springsecuritydemo
 * @name：SecurityConfigTest
 * @Date：2023/9/27 12:08
 * @Filename：SecurityConfigTest
 */
@Configuration
public class SecurityConfigTest extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;

    //注入数据源
    @Autowired
    private DataSource dataSource;

    //配置对象
    @Bean
    public PersistentTokenRepository persistentTokenRepository(){
        JdbcTokenRepositoryImpl jdbcTokenRepository=new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
       //jdbcTokenRepository.setCreateTableOnStartup(true);
        return jdbcTokenRepository;
    }

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
       auth.userDetailsService(userDetailsService).passwordEncoder(password());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //退出
        http.logout().logoutUrl("/logout")
                .logoutSuccessUrl("/test/hello").permitAll();

        //配置没有权限访问跳转自定义页面
        http.exceptionHandling().accessDeniedPage("/unauth.html");

        http.formLogin()//自定义编写登录页面
                .loginPage("/login.html")//登录页面设置
                .loginProcessingUrl("/user/login")//登录访问路径
                .defaultSuccessUrl("/success.html").permitAll()//登录成功后跳转路径
                .and().authorizeRequests()
                .antMatchers("/","/test/hello","/user/login").permitAll()//设置哪些路径可以直接访问，不需要认证
                //当前用户，只有具有damins权限才能访问这个路径
                //1.hasAuthority
//                .antMatchers("/test/index").hasAuthority("admins")
                //2.hasAnyAuthority
//                .antMatchers("/test/index").hasAnyAuthority("admins,manager")
                //3.hasRole        ROLE_sale
//                .antMatchers("/test/index").hasRole("sale")
//                .antMatchers("/test/index").hasAnyRole("sale")
                .anyRequest().authenticated()
                //配置自动登录
                .and().rememberMe().tokenRepository(persistentTokenRepository())
                .tokenValiditySeconds(60)//设置有效时长
                .userDetailsService(userDetailsService)
                .and().csrf().disable();//关闭csrf防护
    }

    //注入PasswordEncoder类到spring容器中
    @Bean
    PasswordEncoder password(){
        return new BCryptPasswordEncoder();
    }
}
